package com.ruoyi.system.service.impl.repair;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.common.VehicleRepairConstant;
import com.ruoyi.system.common.VehicleStateConstant;
import com.ruoyi.system.domain.db.basic.BasicVehicles;
import com.ruoyi.system.domain.db.repair.VehicleRepair;
import com.ruoyi.system.domain.dto.RepairDriverDto;
import com.ruoyi.system.domain.vo.repair.RepairDriverVo;
import com.ruoyi.system.mapper.repair.VehicleRepairMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.service.IVehicleRepairService;

import javax.annotation.Resource;

/**
 * 报修申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class VehicleRepairServiceImpl implements IVehicleRepairService 
{
    @Resource
    private VehicleRepairMapper vehicleRepairMapper;


    /**
     * 查询报修申请
     * 
     * @param repairId 报修申请主键
     * @return 报修申请
     */
    @Override
    public RepairDriverVo selectVehicleRepairByRepairId(Long repairId)
    {
        RepairDriverDto repairDriverDto = vehicleRepairMapper.selectVehicleRepairByRepairId(repairId);
        if (repairDriverDto != null)
        {
            return dtoToVo(repairDriverDto);
        }
        return null;
    }

    /**
     * 查询报修申请列表
     * 
     * @param vehicleRepair 报修申请
     * @return 报修申请
     */
    @Override
    public List<RepairDriverVo> selectVehicleRepairList(VehicleRepair vehicleRepair)
    {

        vehicleRepair.getParams().put("driverId", SecurityUtils.getUserId());
        List<RepairDriverDto> repairDriverDtos = vehicleRepairMapper.selectVehicleRepairList(vehicleRepair);
        if (repairDriverDtos != null)
        {
            return repairDriverDtos.stream().map(this::dtoToVo).collect(Collectors.toList());
        }
        return null;

    }

    /**
     * 新增报修申请
     * 
     * @param vehicleRepair 报修申请
     * @return 结果
     */
    @Override
    public int insertVehicleRepair(VehicleRepair vehicleRepair)
    {

        Date nowDate = DateUtils.getNowDate();
        vehicleRepair.setCreateTime(nowDate);
        vehicleRepair.setCreateBy(SecurityUtils.getUserId());
//        vehicleRepair.setRepairBy(SecurityUtils.getUserId());
        vehicleRepair.setUpdateTime(nowDate);
        vehicleRepair.setState(VehicleRepairConstant.UN_REPAIR);
        return vehicleRepairMapper.insertVehicleRepair(vehicleRepair);
    }

    /**
     * 修改报修申请
     * 
     * @param vehicleRepair 报修申请
     * @return 结果
     */
    @Override
    public int updateVehicleRepair(VehicleRepair vehicleRepair)
    {
        vehicleRepair.setUpdateTime(DateUtils.getNowDate());
        return vehicleRepairMapper.updateVehicleRepair(vehicleRepair);
    }

    /**
     * 批量删除报修申请
     * 
     * @param repairIds 需要删除的报修申请主键
     * @return 结果
     */
    @Override
    public int deleteVehicleRepairByRepairIds(Long[] repairIds)
    {
        return vehicleRepairMapper.deleteVehicleRepairByRepairIds(repairIds);
    }

    /**
     * 删除报修申请信息
     * 
     * @param repairId 报修申请主键
     * @return 结果
     */
    @Override
    public int deleteVehicleRepairByRepairId(Long repairId)
    {
        return vehicleRepairMapper.deleteVehicleRepairByRepairId(repairId);
    }

    /**
     * 查看是否自己拥有汽车
     */
    @Override
    public BasicVehicles hasVehicle() {
        return vehicleRepairMapper.selectCarByUserId(SecurityUtils.getLoginUser().getUserId());
    }

    public RepairDriverVo dtoToVo(RepairDriverDto dto)
    {
        RepairDriverVo vo = new RepairDriverVo();
        vo.setRepairId(dto.getRepairId());
        vo.setReason(dto.getReason());
        vo.setCreateTime(dto.getCreateTime());
        vo.setUpdateTime(dto.getUpdateTime());
        vo.setState(dto.getState());
        vo.setPhone(dto.getPhone());
        vo.setVehicleId(dto.getBasicVehicles().getVehicleId());
        vo.setVehicleNumber(dto.getBasicVehicles().getVehicleNumber());
        vo.setManufacturer(dto.getBasicVehicles().getManufacturer());
        vo.setModel(dto.getBasicVehicles().getModel());
        vo.setCapacity(dto.getBasicVehicles().getCapacity());
        if(dto.getRepairUser()!=null)
        {
            vo.setUserId(dto.getRepairUser().getUserId());
            vo.setUserName(dto.getRepairUser().getUserName());
        }
        return vo;
    }


}
